package com.zhouyilin.bigevent2.controller;


import com.zhouyilin.bigevent2.pojo.Category;
import com.zhouyilin.bigevent2.pojo.Result;
import com.zhouyilin.bigevent2.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController   {

    @Autowired
    private CategoryService categoryService;

//    @PostMapping("/add")
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success();
    }

//    @GetMapping("/list")
    @GetMapping
    public Result<List<Category>> list(){
        List <Category> categories = categoryService.list();
        return Result.success(categories);
    }
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        Category category= categoryService.findById(id);
        return Result.success(category);
    }
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        Integer id = category.getId();
        String categoryName = category.getCategoryName();
        String categoryAlias  = category.getCategoryAlias();
        categoryService.update(id,categoryName,categoryAlias);
        return Result.success();
    }
    @DeleteMapping
    public Result deleteById(Integer id){
        categoryService.deleteById(id);
        return Result.success();
    }

}
